package com.eleadmin.store.biz.impl;

import com.eleadmin.damon.utils.constant.SqlConstant;
import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.store.biz.ProductStorageBiz;
import com.eleadmin.store.entity.ProductStorage;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.store.result.PriceResult;
import com.eleadmin.store.result.SelectResult;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.rmi.CORBA.Util;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2024-02-13 18:44:18
 */
@Repository
public class ProductStorageBizImpl extends BaseDao implements ProductStorageBiz {

    @Override
    public JsonResult list(PageResult2<T> page, ProductStorage productStorage, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<ProductStorage> rm = new BeanPropertyRowMapper<ProductStorage>(ProductStorage.class);
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.warehouseName");
        sql.append(",p.productName,p.code,p.specification,p.lengths,p.negative");
        sql.append(",p.num,p.liWeight,p.liTotalWeight,p.price");
        sql.append(",p.model,p.locality,p.orderNum,p.remark");
        sql.append(",p.create_time,p.create_user,p.update_time,p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from we_product_storage p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=p.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
        sql.append(" where p.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(productStorage)) {
            if (Utils.isNotEmpty(productStorage.getWarehousename())) {
                sql.append(" and instr(p.warehouseName,:warehouseName)>0 ");
                map.put("warehouseName", productStorage.getWarehousename());
            }
            if (Utils.isNotEmpty(productStorage.getProductname())) {
                sql.append(" and (instr(p.productName,:productName)>0 or instr(p.code,:productName)>0) ");
                map.put("productName", productStorage.getProductname());
            }
            if (Utils.isNotEmpty(productStorage.getSpecification())) {
                sql.append(" and instr(p.specification,:specification)>0 ");
                map.put("specification", productStorage.getSpecification());
            }
            if (Utils.isNotEmpty(productStorage.getSort()) && Utils.isNotEmpty(productStorage.getOrder())) {
                sql.append(" order by p." + productStorage.getSort() + " " + productStorage.getOrder());
            } else {
                sql.append(" order by p.num desc");
            }
        }
        JSONObject data = jsonPage2(getPageQueryList(page, sql.toString(), map, rm));
        return JsonResult.ok().setData(data);
    }

    @Override
    public PriceResult storageNums(ProductStorage productStorage, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<PriceResult> rm = new BeanPropertyRowMapper<PriceResult>(PriceResult.class);
        StringBuffer sql = new StringBuffer("select  ");
        sql.append(" ROUND(IFNULL(sum(p.liTotalWeight),0),3) liTotalWeight ");
        sql.append(" from we_product_storage p ");
        sql.append(" where p.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(productStorage)) {
            if (Utils.isNotEmpty(productStorage.getWarehousename())) {
                sql.append(" and instr(p.warehouseName,:warehouseName)>0 ");
                map.put("warehouseName", productStorage.getWarehousename());
            }
            if (Utils.isNotEmpty(productStorage.getProductname())) {
                sql.append(" and (instr(p.productName,:productName)>0 or instr(p.code,:productName)>0) ");
                map.put("productName", productStorage.getProductname());
            }
            if (Utils.isNotEmpty(productStorage.getSpecification())) {
                sql.append(" and instr(p.specification,:specification)>0 ");
                map.put("specification", productStorage.getSpecification());
            }
        }
        getMapParameter(sql, map);
        List<PriceResult> priceResults = jdbcTemplate.query(sql.toString(), map, rm);
        return Utils.isNotEmpty(priceResults)?priceResults.get(0): null;
    }

    @Override
    public List<ProductStorage> list(ProductStorage productStorage, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<ProductStorage> rm = new BeanPropertyRowMapper<ProductStorage>(ProductStorage.class);
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.warehouseName");
        sql.append(",p.productName,p.code,p.specification,p.lengths,p.negative");
        sql.append(",p.num,p.liWeight,p.liTotalWeight,p.price");
        sql.append(",p.model,p.locality,p.orderNum,p.remark");
        sql.append(",p.create_time,p.create_user,p.update_time,p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from we_product_storage p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
        sql.append(" where p.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(productStorage)) {
            if (Utils.isNotEmpty(productStorage.getWarehousename())) {
                sql.append(" and instr(p.warehouseName,:warehouseName)>0 ");
                map.put("warehouseName", productStorage.getWarehousename());
            }
            if (Utils.isNotEmpty(productStorage.getProductname())) {
                sql.append(" and (instr(p.productName,:productName)>0 or instr(p.code,:productName)>0) ");
                map.put("productName", productStorage.getProductname());
            }
            if (Utils.isNotEmpty(productStorage.getSpecification())) {
                sql.append(" and instr(p.specification,:specification)>0 ");
                map.put("specification", productStorage.getSpecification());
            }
            if (Utils.isNotEmpty(productStorage.getSort()) && Utils.isNotEmpty(productStorage.getOrder())) {
                sql.append(" order by p." + productStorage.getSort() + " " + productStorage.getOrder());
            } else {
                sql.append(" order by p.num desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public List<SelectResult> listSel(ProductStorage productStorage, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<SelectResult> rm = new BeanPropertyRowMapper<SelectResult>(SelectResult.class);
        StringBuffer sql = new StringBuffer("select p.id value,p.warehouseName");
        sql.append(",CONCAT(p.productName,'-', p.specification, '-', p.lengths, '-', p.negative) label,p.productName productName,p.code,p.specification,p.lengths,p.negative");
        sql.append(",p.num,p.liWeight,p.liTotalWeight,p.price");
        sql.append(",p.model,p.locality,p.orderNum,p.remark");
        sql.append(",p.create_time,p.create_user,p.update_time,p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from we_product_storage p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
        sql.append(" where p.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(productStorage)) {
            if (Utils.isNotEmpty(productStorage.getWarehousename())) {
                sql.append(" and (instr(p.productName,:warehousename)>0 or instr(p.productName,:warehousename)>0  or instr(p.code,:warehousename)>0) ");
                map.put("warehousename", productStorage.getWarehousename());
            }
            if (Utils.isNotEmpty(productStorage.getSort()) && Utils.isNotEmpty(productStorage.getOrder())) {
                sql.append(" order by p." + productStorage.getSort() + " " + productStorage.getOrder());
            } else {
                sql.append(" order by p.num desc");
            }
        }
        sql.append(" limit "+ SqlConstant.LIMIT);
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public ProductStorage productStorageNum(ProductStorage productStorage, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<ProductStorage> rm = new BeanPropertyRowMapper<ProductStorage>(ProductStorage.class);
        StringBuffer sql = new StringBuffer("select p.id,p.warehouseName");
        sql.append(",p.productName,p.code,p.specification,p.lengths,p.negative");
        sql.append(",p.num,p.liWeight,p.liTotalWeight,p.price");
        sql.append(",p.model,p.locality,p.orderNum,p.remark");
        sql.append(" from we_product_storage p ");
        sql.append(" where p.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(productStorage)) {
            if (Utils.isNotEmpty(productStorage.getWarehousename())) {
                sql.append(" and p.warehouseName=:warehouseName ");
                map.put("warehouseName", productStorage.getWarehousename());
            }
            if (Utils.isNotEmpty(productStorage.getProductname())) {
                sql.append(" and p.productName=:productName ");
                map.put("productName", productStorage.getProductname());
            }
            if (Utils.isNotEmpty(productStorage.getSpecification())) {
                sql.append(" and p.specification=:specification ");
                map.put("specification", productStorage.getSpecification());
            }
            if (Utils.isNotEmpty(productStorage.getLengths())) {
                sql.append(" and p.lengths=:lengths ");
                map.put("lengths", productStorage.getLengths());
            }
            if (Utils.isNotEmpty(productStorage.getNegative())) {
                sql.append(" and p.negative=:negative ");
                map.put("negative", productStorage.getNegative());
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public ProductStorage getById(String id) {
        RowMapper<ProductStorage> rm = new BeanPropertyRowMapper<ProductStorage>(ProductStorage.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.warehouseName");
        sql.append(",p.productName,p.code,p.specification,p.lengths,p.negative");
        sql.append(",p.num,p.liWeight,p.liTotalWeight,p.price");
        sql.append(",p.model,p.locality,p.orderNum,p.remark");
        sql.append(",p.create_time,p.create_user,p.update_time,p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from we_product_storage p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
        sql.append(" where p.deleted=0 and p.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        try {
            return jdbcTemplate.queryForObject(sql.toString(), map, rm);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public ProductStorage getByName(ProductStorage productStorage, User loginUser) {
        RowMapper<ProductStorage> rm = new BeanPropertyRowMapper<ProductStorage>(ProductStorage.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.warehouseName");
        sql.append(",p.productName,p.code,p.specification,p.lengths,p.negative");
        sql.append(",p.num,p.liWeight,p.liTotalWeight,p.price");
        sql.append(",p.model,p.locality,p.orderNum,p.remark");
        sql.append(",p.create_time,p.create_user,p.update_time,p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from we_product_storage p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
        sql.append(" where p.deleted=0 ");
        sql.append(" and p.organization_id=:tableOrganizationId ");
        map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        sql.append(" and p.warehouseName=:warehouseName ");
        map.put("warehouseName", productStorage.getWarehousename());
        sql.append(" and p.productName=:productName ");
        map.put("productName", productStorage.getProductname());
        sql.append(" and p.specification=:specification ");
        map.put("specification", productStorage.getSpecification());
        sql.append(" and p.lengths=:lengths ");
        map.put("lengths", productStorage.getLengths());
        sql.append(" and p.negative=:negative ");
        map.put("negative", productStorage.getNegative());
        getMapParameter(sql, map);
        try {
            return jdbcTemplate.queryForObject(sql.toString(), map, rm);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean save(ProductStorage productStorage, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into we_product_storage (");
        sql.append("organization_id,warehouseName,productName,code,specification");
        sql.append(",lengths,negative,num,liWeight");
        sql.append(",liTotalWeight,price,model,locality");
        sql.append(",orderNum,remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:warehouseName,:productName,:code,:specification");
        sql.append(",:lengths,:negative,:num,:liWeight");
        sql.append(",:liTotalWeight,:price,:model,:locality");
        sql.append(",:orderNum,:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("warehouseName", productStorage.getWarehousename());
        map.put("productName", productStorage.getProductname());
        map.put("code", productStorage.getCode());
        map.put("specification", productStorage.getSpecification());
        map.put("lengths", productStorage.getLengths());
        map.put("negative", productStorage.getNegative());
        map.put("num", productStorage.getNum() + "");
        map.put("liWeight", productStorage.getLiweight());
        map.put("liTotalWeight", productStorage.getLitotalweight() + "");
        map.put("price", productStorage.getPrice());
        map.put("model", productStorage.getModel());
        map.put("locality", productStorage.getLocality());
        map.put("orderNum", productStorage.getOrdernum() == null ? null : productStorage.getOrdernum() + "");
        map.put("remark", productStorage.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(ProductStorage productStorage, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update we_product_storage set ");
        sql.append("code=:code, num=:num");
        sql.append(",liWeight=:liWeight,liTotalWeight=:liTotalWeight,price=:price ");
        sql.append(",model=:model,locality=:locality,orderNum=:orderNum ");
        sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
        map.put("code", productStorage.getCode());
        map.put("num", productStorage.getNum() + "");
        map.put("liWeight", productStorage.getLiweight());
        map.put("liTotalWeight", productStorage.getLitotalweight() + "");
        map.put("price", productStorage.getPrice());
        map.put("model", productStorage.getModel());
        map.put("locality", productStorage.getLocality());
        map.put("orderNum", productStorage.getOrdernum() == null ? null : productStorage.getOrdernum() + "");
        map.put("remark", productStorage.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", productStorage.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean updateNum(ProductStorage productStorage, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update we_product_storage set ");
        sql.append("num=num+(:num)");
        if (Utils.isNotEmpty(productStorage.getCode())) {
            sql.append(",code=:code");
        }
        sql.append(",liTotalWeight=liTotalWeight+(:liTotalWeight)");
        sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("num", productStorage.getNum() + "");
        if (Utils.isNotEmpty(productStorage.getCode())) {
            map.put("code", productStorage.getCode());
        }
        map.put("liTotalWeight", productStorage.getLitotalweight() + "");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", productStorage.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update we_product_storage set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
